import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_app/global/color_config.dart';
import 'package:flutter_app/main/main/model/system_system_entity.dart';
import 'package:flutter_app/main/main/system/model/system_model.dart';
import 'package:flutter_app/main/main/system/system_details_table.dart';

class SystemDetails extends StatefulWidget {
  static const RouteName = "/main/system/system_details";

  const SystemDetails({super.key});

  @override
  SystemDetailsState createState() => SystemDetailsState();
}

class SystemDetailsState extends State<SystemDetails>
    with SingleTickerProviderStateMixin {
  late TabController tabController;
  List<SystemSystemDataChildren> titleList = [];
  int count = 0;
  String title = '';

  @override
  Widget build(BuildContext context) {
    final systemModel =
        ModalRoute.of(context)!.settings.arguments as SystemModel;

    count = systemModel.children.length;
    title = systemModel.title;
    titleList = systemModel.children;
    tabController = TabController(length: count, vsync: this);
    tabController.index = systemModel.index;

    return Scaffold(
      appBar: AppBar(
        backgroundColor: ColorConfig.themeColor,
        elevation: 0,
        centerTitle: true,
        title: Text(
          title,
          style: const TextStyle(fontSize: 18, color: Colors.white),
        ),
        leading: IconButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            icon: const Icon(
              Icons.arrow_back_rounded,
              color: Colors.white,
            )),
      ),
      body: Flex(
        direction: Axis.vertical,
        children: [
          Container(
            width: double.infinity,
            color: ColorConfig.themeColor,
            child: TabBar(
              controller: tabController,
              padding: const EdgeInsets.only(bottom: 8),
              unselectedLabelColor: Colors.white54,
              unselectedLabelStyle: const TextStyle(fontSize: 15),
              labelStyle: const TextStyle(fontSize: 15),
              physics: const BouncingScrollPhysics(),
              indicatorWeight: 0.1,
              isScrollable: true,
              tabs: titleList.map((e) => Tab(text: e.name)).toList(),
            ),
          ),
          Expanded(
            child: TabBarView(
              //构建
              controller: tabController,
              children: titleList.map((e) {
                return SystemDetailsWidget(e.id);
              }).toList(),
            ),
          )
        ],
      ),
    );
  }
}